package com.sl.hx.shop.ui.classify.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseActivity;
import com.sl.hx.shop.bean.SearchResult;
import com.sl.hx.shop.ui.classify.adapter.SearchResultAdapter;
import com.sl.hx.shop.ui.classify.contacts.SearchResultContacts;
import com.sl.hx.shop.ui.classify.presenter.SearchResultPresenter;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//搜索结果页
public class SearchResultActivity extends BaseActivity<SearchResultContacts.view, SearchResultPresenter> implements SearchResultContacts.view {


    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.iv_zh_up)
    ImageView ivZhUp;
    @BindView(R.id.iv_zh_down)
    ImageView ivZhDown;
    @BindView(R.id.iv_price_up)
    ImageView ivPriceUp;
    @BindView(R.id.iv_price_down)
    ImageView ivPriceDown;
    @BindView(R.id.iv_volume_up)
    ImageView ivVolumeUp;
    @BindView(R.id.iv_volune_down)
    ImageView ivVoluneDown;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.tvzh)
    TextView tvzh;
    @BindView(R.id.tvprice)
    TextView tvprice;
    @BindView(R.id.tvvolume)
    TextView tvvolume;

    private String mSearchTitle;//关键词
    private SearchResultAdapter mSearchResultAdapter; //适配器
    private List<SearchResult> searchResults = new ArrayList<>(); //适配器数据



    private int type=2; //排序类型  综合1:desc,2:asc 价格3:desc,4:asc 销量5:desc,6:asc

    private boolean zhClick = true;
    private boolean priceClick = true;
    private boolean volumClick = true;

    private boolean mIsRefresh = true;//刷新
    private boolean mIsLoadmore = true; //加载更多

    private int page = 1;   //默认页码
    private int num = 10;   //默认每页个数


    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected SearchResultPresenter createPresenter() {
        return new SearchResultPresenter(mContext);
    }

    @Override
    protected void initView() {
        //分类页传过来的标题,当关键词使用
        mSearchTitle = getIntent().getStringExtra("searchTitle");
        //设置标题
        title.setText(mSearchTitle);

        refresh();
        initRecycle();
        //默认情况数据请求
        mPresenter.getSearch(mSearchTitle, type, page, num);
    }


    private void initRecycle() {
        mSearchResultAdapter = new SearchResultAdapter(searchResults);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerView.setAdapter(mSearchResultAdapter);
        mRecyclerView.addItemDecoration(new MarginDecoration(mContext));
        mSearchResultAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mIsRefresh=false;
                mPresenter.getSearch(mSearchTitle, type, page, num);
            }
        });
        mSearchResultAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, GoodsDetailActivity.class);
                intent.putExtra("user_type",mSearchResultAdapter.getItem(position).getUser_type());
                intent.putExtra("num_iid",mSearchResultAdapter.getItem(position).getNum_iid());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void ShowResultList(List<SearchResult> searchResultList) {
        if (mIsRefresh) {
            mSearchResultAdapter.setNewData(searchResultList);
        }else {
            if (searchResultList != null && searchResultList.size() > 0) {
                mSearchResultAdapter.addData(searchResultList);
            }
        }

        if (searchResultList.size() < num) {
            mSearchResultAdapter.loadMoreEnd(true);
        } else {
            mSearchResultAdapter.loadMoreComplete();
        }

        //关闭刷新
        if (mRefresh.isRefreshing()){
            mRefresh.setRefreshing(false);
        }
    }


    @OnClick({R.id.iv_back, R.id.ll_zh, R.id.ll_price, R.id.ll_volume})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            //综合
            case R.id.ll_zh:
                if (zhClick) {
                    setcolor();
                    //第一次点击按降序排
                    type=1;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    //改变布局颜色
                    tvzh.setTextColor(getResources().getColor(R.color.ff666));
                    ivZhDown.setImageResource(R.drawable.s_icon_selected_donw);
                    ivZhUp.setImageResource(R.drawable.s_icon_normal_up);
                    zhClick = false;
                } else {
                    setcolor();
                    //再次点击按升序排
                    type=2;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    tvzh.setTextColor(getResources().getColor(R.color.ff666));
                    ivZhDown.setImageResource(R.drawable.s_icon_normal_donw);
                    ivZhUp.setImageResource(R.drawable.s_icon_selected_up);
                    zhClick = true;
                }
                break;
            //价格
            case R.id.ll_price:
                if (priceClick) {
                    setcolor();
                    //第一次点击按升序排
                    type=4;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    //改变布局颜色
                    tvprice.setTextColor(getResources().getColor(R.color.ff666));
                    ivPriceUp.setImageResource(R.drawable.s_icon_selected_up);
                    ivPriceDown.setImageResource(R.drawable.s_icon_normal_donw);
                    priceClick = false;
                } else {
                    setcolor();
                    //再次点击按降序排
                    type=3;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    tvprice.setTextColor(getResources().getColor(R.color.ff666));
                    ivPriceUp.setImageResource(R.drawable.s_icon_normal_up);
                    ivPriceDown.setImageResource(R.drawable.s_icon_selected_donw);
                    priceClick = true;
                }
                break;
            //销量
            case R.id.ll_volume:
                if (volumClick) {
                    setcolor();
                    //第一次点击按升序排
                    type=5;
                    mIsRefresh=true;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    //改变布局颜色
                    tvvolume.setTextColor(getResources().getColor(R.color.ff666));
                    ivVoluneDown.setImageResource(R.drawable.s_icon_normal_donw);
                    ivVolumeUp.setImageResource(R.drawable.s_icon_selected_up);
                    volumClick = false;
                } else {
                    setcolor();
                    //再次点击按降序排
                    type=6;
                    mIsRefresh=true;
                    mPresenter.getSearch(mSearchTitle, type, page, num);
                    tvvolume.setTextColor(getResources().getColor(R.color.ff666));
                    ivVoluneDown.setImageResource(R.drawable.s_icon_selected_donw);
                    ivVolumeUp.setImageResource(R.drawable.s_icon_normal_up);
                    volumClick = true;
                }
                break;
        }
    }

    private void refresh() {
        mRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.main_color));
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mIsRefresh=true;
                page=1;
                mPresenter.getSearch(mSearchTitle, type, page, num);
            }
        });

    }

    //初始化布局颜色
    private void setcolor() {
        tvzh.setTextColor(getResources().getColor(R.color.dddddd));
        tvprice.setTextColor(getResources().getColor(R.color.dddddd));
        tvvolume.setTextColor(getResources().getColor(R.color.dddddd));
        ivZhUp.setImageResource(R.drawable.s_icon_normal_up);
        ivZhDown.setImageResource(R.drawable.s_icon_normal_donw);
        ivPriceUp.setImageResource(R.drawable.s_icon_normal_up);
        ivPriceDown.setImageResource(R.drawable.s_icon_normal_donw);
        ivVolumeUp.setImageResource(R.drawable.s_icon_normal_up);
        ivVoluneDown.setImageResource(R.drawable.s_icon_normal_donw);
    }


    //recycleview item设置间距
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margintop, marginleft, marginright;

        public MarginDecoration(Context context) {
            margintop = ScreenUtil.dp2px(context, 8);
            marginleft = ScreenUtil.dp2px(context, 16);
            marginright = ScreenUtil.dp2px(context, 4);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) % 2 == 0) {
                outRect.set(marginleft, 0, marginright, margintop);
            } else {
                outRect.set(marginright, 0, marginleft, margintop);
            }
        }
    }

}
